package com.lhj.crm.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhj.crm.entity.Employee;
import com.lhj.crm.entity.Role;
import com.lhj.crm.service.IRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author FYI
 * @since 2020-03-23
 */
@RestController
public class RoleController {

    private Logger log = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private IRoleService roleService;

    @GetMapping("/role")
    public List<Role> roles(){

        return roleService.list();
    }
    @GetMapping("/role/ListPage")
    public Map getList(Integer page,Integer limit ){
        Map<String,Object> map = new HashMap<String,Object> ();

        //设置mybatisPlus分页
        Page<Role> rolePage = new Page<Role>();
        //设置每页记录数
        rolePage.setSize(limit);
        //设置当前页码
        rolePage.setCurrent(page);

        IPage<Role> iPage = roleService.selectPageList(rolePage);

        map.put("code",0);
        map.put("msg","succeed");
        map.put("data",iPage.getRecords());
        map.put("count",iPage.getTotal());

        return map;
    }

    //添加
    @PostMapping("/role")
    public Map RoleAdd(Role role,Integer permId,Integer deptId){
        Map<String,Object> map = new HashMap<String,Object>();
        Integer add = roleService.addRole(role, permId,deptId);
        map.put("state",add);
        return map;
    }

    @PutMapping("/role")
    public Map RoleUpdate(Role role,Integer deptId,Integer permId) {
        Map<String, Object> result = new HashMap<String, Object>();
        Integer update = roleService.update(role,deptId, permId);
        result.put("state",update);
        return result;
    }


    @DeleteMapping("/roles/del/{roleId}")
    public Map RoleDel(@PathVariable Integer roleId){
        Role role = new Role();
        role.setRoleId(roleId);
        role.setIsDel(1);
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("state",roleService.updateById(role));
        return result;
    }
}
